<script>
import { getBookcityJsonData, getBookcityData } from "/assets/api/bookcity.js";
import { onBeforeMount, reactive, ref, onMounted, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
export default {
  setup() {
    const router = useRouter();
    let listObj = reactive({
      HotCategories: null,
      activityBanner: null,
      activityList: null,
      bannerList: null,
      completBookList: null,
      freeBook: null,
      hotList: null,
      hottest: null,
    });
    // 数据初始化
    let dateInit = async () => {
      let data = await getBookcityData().catch(() =>
        console.log("接口调用失败")
      );
      if (!data) data = await getBookcityJsonData();
      console.log(data);
      listObj.HotCategories = JSON.parse(JSON.stringify(data.HotCategories));
      listObj.activityBanner = JSON.parse(JSON.stringify(data.activityBanner));
      listObj.activityList = JSON.parse(JSON.stringify(data.activityList));
      listObj.bannerList = JSON.parse(JSON.stringify(data.bannerList));
      listObj.completBookList = JSON.parse(
        JSON.stringify(data.completBookList)
      );
      listObj.freeBook = JSON.parse(JSON.stringify(data.freeBook));
      listObj.hotList = JSON.parse(JSON.stringify(data.hotList));
      listObj.hottest = JSON.parse(JSON.stringify(data.hottest));
    };
    // 定义响应式数据 服务于滚动条
    let BookCityRef = ref(null);
    let Alpha = reactive({
      opacity: 0,
    });

    // 滚轮事件
    let __scrollInit = () => {
      // let target = BookCityRef.value.parentElement;
      let target = document.querySelector("#app");
      // console.dir("滚动视图原生对象", target);

      target.onscroll = (e) => {
        // console.dir(e.target.scrollTop);
        let scrollTop = e.target.scrollTop;
        let top = 200;
        Alpha.opacity = scrollTop / top;
        // console.log(Alpha);
      };
    };

    let __scrollDestroy = () => {
      // let target = BookCityRef.value.parentElement;
      let target = document.querySelector("#app");
      target.onscroll = null;
      console.log("成功清除滚动条事件");
    };

    // 搜索事件
    let searchFun = () => {
      router.push("/pages/SearchView");
    };

    // 定义响应式数据 服务与轮播图
    let BannerRef = ref(null);
    let bannerChangeEve = (index) => {
      // console.log(index, BannerRef.value);
      BannerRef.value.swipeTo(index);
    };

    // 过滤器
    let filters_simplify = (val) => {
      if (val > 10000) return (val / 10000).toFixed(1) + "w";
      if (val > 1000) return (val / 1000).toFixed(1) + "k";

      return val;
    };

    // 挂载前
    onBeforeMount(() => {
      // 数据初始化
      dateInit();
    });

    // 挂载后
    onMounted(() => {
      // 滚轮事件
      __scrollInit();
    });

    // 销毁前
    onBeforeUnmount(() => {
      // 滚轮事件清除
      __scrollDestroy();
    });

    // 分类页面跳转
    let category = () => {
      router.push("/pages/CategoryView");
    };

    let ranking = () => {
      router.push("/pages/RankingView");
    };

    let move = () => {
      router.push("/pages/CategoryView");
    };

    // 跳到详情页
    let details = (id) => {
      router.push({ path: "/pages/DetailsView", query: { id: id } });
    };

    let end = () => {
      router.push("/pages/EndView");
    };

    return {
      details, // 跳到详情页
      listObj,
      BookCityRef,
      Alpha,
      searchFun,
      filters_simplify, //过滤器
      bannerChangeEve, //轮播图滚动事件
      BannerRef, // 轮播图原生对象ref
      category, //分类页面跳转
      ranking, //排行榜
      move,
      end,
      // detalis
    };
  },
};
</script>

<template>
  <div class="BookCity" ref="BookCityRef">
    <!-- 轮播图 -->
    <div class="cabinet-box">
      <div class="cabinet_top">
        <van-swipe
          :autoplay="3000"
          indicator-color="white"
          @change="bannerChangeEve"
        >
          <van-swipe-item
            class="img"
            @click="details(item.BookId)"
            v-for="item in listObj.bannerList"
            :key="item.BookId"
          >
            <img :src="item.BookImage" alt="" />
          </van-swipe-item>
        </van-swipe>
        <div class="search">
          <div class="title" :style="{ opacity: Alpha.opacity }">
            <h3>发现</h3>
            <em class="iconfont icon-31sousuo" @click="searchFun"></em>
          </div>
          <div class="inp" @click="searchFun">
            <span class="iconfont icon-31sousuo"></span>
            <p>搜索相关内容</p>
          </div>
        </div>
      </div>
      <div class="cabinet_tab">
        <van-swipe
          ref="BannerRef"
          class="my-swipe-bg"
          :touchable="false"
          :show-indicators="false"
          :loop="true"
        >
          <van-swipe-item
            class="img"
            v-for="item in listObj.bannerList"
            :key="item.BookId"
          >
            <img :src="item.BookImage" alt="" />
          </van-swipe-item>
        </van-swipe>
        <div class="tab_item" @click="category">
          <div class="img"></div>
          <p>分类</p>
        </div>
        <div class="tab_item" @click="end">
          <div class="img"></div>
          <p>专题</p>
        </div>
        <div class="tab_item" @click="ranking">
          <div class="img"></div>
          <p>排行</p>
        </div>
        <div class="tab_item" @click="end">
          <div class="img"></div>
          <p>完本</p>
        </div>
        <div class="tab_item" @click="end">
          <div class="img"></div>
          <p>免费</p>
        </div>
      </div>
    </div>

    <!-- 今日最热 -->
    <div class="free" v-if="listObj.hottest">
      <div class="welfare_top">
        <p class="we">今日最热</p>
        <p class="move" @click="move">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="bottom" @click="details(listObj.hottest.BookId)">
        <div class="top_title">
          <p>{{ listObj.hottest.BookName }}</p>
          <p>{{ listObj.hottest.Author }}</p>
        </div>
        <div class="bottom_title">
          <span>“</span>
          <p class="text-ellipsis-3">
            {{ listObj.hottest.Description }}
          </p>
        </div>
      </div>
      <div class="free_img" @click="details(listObj.hottest.BookId)">
        <img :src="listObj.hottest.BookImage" alt="" />
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="item in listObj.hotList"
          :key="item.BookId"
          @click="details(item.BookId)"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
            <span class="over" v-if="item.isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">{{ item.BookName }}</p>
            <p class="names">{{ item.Author }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 热门分类 -->
    <div class="hot" v-if="listObj.HotCategories">
      <div class="welfare_top">
        <p class="we">热门分类</p>
        <p class="move" @click="move">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="people">
        <div
          class="people_item"
          v-for="item in listObj.HotCategories"
          :key="item.categoryId"
          @click="move"
        >
          <div class="title">
            <p class="name">{{ item.tag }}</p>
            <p class="num">{{ filters_simplify(item.bookTotal) }} 本书</p>
          </div>
          <img class="small_img" :src="item.BookImageList[0]" alt="" />
          <img class="big_img" :src="item.BookImageList[1]" alt="" />
        </div>
      </div>
    </div>

    <!-- 专题活动 -->
    <div class="play" v-if="listObj.activityBanner && listObj.activityList">
      <div class="welfare_top">
        <p class="we">专题活动</p>
        <p class="move" @click="move">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="play_img" @click="details(listObj.activityBanner.BookId)">
        <img :src="listObj.activityBanner.BookImage" alt="" />
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="item in listObj.activityList"
          :key="item.BookId"
          @click="details(item.BookId)"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
            <span class="over" v-if="item.isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">{{ item.BookName }}</p>
            <p class="names">{{ item.Author }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 热门完本 -->
    <div class="play" v-if="listObj.completBookList">
      <div class="welfare_top">
        <p class="we">热门完本</p>
        <p class="move" @click="move">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="item in listObj.completBookList"
          :key="item.BookId"
          @click="details(item.BookId)"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
            <span class="over" v-if="item.isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">{{ item.BookName }}</p>
            <p class="names">{{ item.Author }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 热门免费 -->
    <div class="play" v-if="listObj.completBookList">
      <div class="welfare_top">
        <p class="we">热门免费</p>
        <p class="move" @click="move">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="item in listObj.freeBook.imgList"
          :key="item.BookId"
          @click="details(item.BookId)"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
            <span class="over" v-if="item.isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">{{ item.BookName }}</p>
            <p class="names">{{ item.Author }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 连载 -->
    <div class="serials" v-if="listObj.completBookList">
      <div
        class="serials_item"
        v-for="item in listObj.freeBook.textList"
        :key="item.BookId"
        @click="details(item.BookId)"
      >
        <div class="wem">
          <p class="left">{{ item.tag }} |</p>
          <p class="left_s">{{ item.latestTitle }}</p>
        </div>
        <div class="over">
          <p class="right">{{ item.Author }}</p>
          <p v-if="!item.isEnd" class="right_s">连载中</p>
          <p v-if="item.isEnd" class="over_right">已完结</p>
        </div>
      </div>
    </div>

    <BottomNavigationBar current="1" />
  </div>
</template>

<style lang="scss" scoped>
.BookCity {
  min-height: 100%;
  min-width: 375px;
  max-width: 750px;
  width: 100%;
  box-sizing: border-box;
  padding-bottom: 65px;
  .cabinet-box {
    overflow: hidden;
    background-color: rgba($color: #fff, $alpha: 0.85);
  }
  .cabinet_top {
    // height: 240px;
    position: relative;
    overflow: hidden;
    border-bottom-left-radius: 83% 50px;
    border-bottom-right-radius: 83% 50px;
    .van-swipe {
      // height: 240px;
      width: 100%;
      background-color: #10101f;
      img {
        object-fit: cover;
      }
    }
    .search {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      padding: 0 27px;
      width: 100%;
      box-sizing: border-box;
      height: 50px;
      display: flex;
      align-items: center;

      .title {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 20px;
        box-sizing: border-box;
        color: rgba(0, 0, 0, 0.98);
        background-color: rgba(255, 255, 255, 0.96);
        opacity: 0;
        z-index: 8;
        box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.2);

        h3 {
          font-size: 18px;
          font-weight: bold;
        }
        .icon-31sousuo {
          font-size: 20px;
        }

        .iconfont {
          position: absolute;
          right: 20px;
        }
      }

      .inp {
        display: flex;
        height: 34px;
        align-items: center;
        border-radius: 8px;
        background-color: #fff;
        opacity: 0.8;
        width: 100%;
        box-sizing: border-box;
        box-shadow: 0 0 2px 0px rgba(0, 0, 0, 0.2);
        z-index: 10;
        .icon-31sousuo {
          margin-left: 17px;
          margin-right: 88px;
          color: #999;
          font-size: 15px;
        }
        p {
          font-size: 13px;
          color: #999;
        }
      }
    }
  }
  .cabinet_tab {
    padding: 10px 22px;
    display: flex;
    justify-content: space-between;
    position: relative;
    // background-color: rgba(255, 255, 255, 0.8);
    .tab_item {
      width: 54px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .img {
        border-radius: 50%;
        overflow: hidden;
      }
      &:nth-child(2) {
        .img {
          background: url(/assets/img/bg.png) no-repeat 0 -1px;
          width: 58px;
          height: 54px;
        }
      }
      &:nth-child(3) {
        .img {
          background: url(/assets/img/bg.png) no-repeat -72px -1px;
          width: 55px;
          height: 54px;
        }
      }
      &:nth-child(4) {
        .img {
          background: url(/assets/img/bg.png) no-repeat -136px -1px;
          width: 64px;
          height: 54px;
        }
      }
      &:nth-child(5) {
        .img {
          background: url(/assets/img/bg.png) no-repeat -208px 0;
          width: 64px;
          height: 55px;
        }
      }
      &:nth-child(6) {
        .img {
          background: url(/assets/img/bg.png) no-repeat -281px -1px;
          width: 55px;
          height: 54px;
        }
      }
      p {
        font-size: 12px;
        color: #51515e;
        margin-top: 5px;
      }
    }
  }
  .free {
    padding: 0 20px;
    margin-top: 20px;
    position: relative;
    .welfare_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .we {
        font-size: 18px;
        color: #28282e;
        font-weight: bold;
      }
      .move {
        font-size: 12px;
        color: #9ea0ad;
        .icon-xiangyou1 {
          font-size: 10px;
        }
      }
      .i {
        position: absolute;
        left: 0;
        top: 5px;
        width: 4px;
        height: 18px;
        background-color: #f1325e;
      }
    }
    .bottom {
      margin-top: 25px;
      height: 120px;
      box-shadow: 1px 0px 10px 2px #f6f5f5;
      border-radius: 6px;
      padding-left: 20px;
      padding-top: 16px;
      box-sizing: border-box;
      .top_title {
        display: flex;
        p {
          &:nth-child(1) {
            margin-right: 15px;
            font-size: 14px;
            color: #383f49;
            font-weight: bold;
          }
          &:nth-child(2) {
            transform: scale(0.7);
            color: #b9b4b8;
          }
        }
      }
      .bottom_title {
        margin-top: 10px;
        display: flex;
        width: 200px;

        span {
          color: #ececec;
          font-size: 30px;
        }
        p {
          color: #b6b6bf;
          font-size: 12px;
          transform: scale(0.9);
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
    .free_img {
      width: 80px;
      height: 120px;
      position: absolute;
      top: 37px;
      right: 31px;
      img {
        position: absolute;
        display: block;
        width: 100%;
        transform: skew(0deg, -5deg) rotateY(15deg);
        box-shadow: 2px 2px 2px 1px #dab4b4;
      }
    }
    .free_box {
      display: flex;
      flex-wrap: wrap;
      .free_item {
        margin-top: 19px;
        width: 100px;
        margin-right: calc((100% - 300px) / 2);
        &:nth-child(3n) {
          margin-right: 0;
        }
        .img {
          width: 100%;
          position: relative;
          height: 130px;
          .over {
            position: absolute;
            bottom: 8px;
            right: 7px;
            color: #fff;
            opacity: 0.7;
            background-color: rgba(0, 0, 0, 0.5);
            font-size: 11px;
            height: 20px;
            line-height: 20px;
            padding: 0 5px;
            border-radius: 2px;
          }
        }
        .p {
          display: flex;
          flex-direction: column;
          margin-top: 6px;
          .name {
            font-size: 12px;
            color: #686878;
          }
          .names {
            transform: scale(0.6);
            color: #aaaaad;
            margin-left: -24px;
          }
        }
      }
    }
  }
  .hot {
    padding: 0 20px;
    margin-top: 20px;
    position: relative;
    .welfare_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      .we {
        font-size: 18px;
        color: #28282e;
        font-weight: bold;
      }
      .move {
        font-size: 12px;
        color: #9ea0ad;
        .icon-xiangyou1 {
          font-size: 10px;
        }
      }
      .i {
        position: absolute;
        left: 0;
        top: 5px;
        width: 4px;
        height: 18px;
        background-color: #f1325e;
      }
    }
    .people {
      display: flex;
      flex-wrap: wrap;
      .people_item {
        width: 47%;
        margin-right: 18px;
        position: relative;
        &:nth-child(2n) {
          margin-right: 0;
        }
        height: 96px;
        margin-bottom: 15px;
        border-radius: 6px;
        background-color: #f7f7f9;
        .title {
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          margin-top: 21px;
          .name {
            font-size: 15px;
            font-weight: bold;
            color: #5a5062;
            margin-left: 17px;
          }
          .num {
            transform: scale(0.8);
            color: #a9acba;
            margin-left: 10px;
            margin-top: 3px;
          }
        }
        .small_img {
          height: 46px;
          width: 40px;
          position: absolute;
          right: 9px;
          top: 26px;
        }
        .big_img {
          height: 67px;
          width: 50px;
          position: absolute;
          right: 21px;
          top: 16px;
          box-shadow: 2px 1px 8px #8f8989;
        }
      }
    }
  }
  .play {
    padding: 0 20px;
    margin-top: 20px;
    position: relative;
    .welfare_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      .we {
        font-size: 18px;
        color: #28282e;
        font-weight: bold;
      }
      .move {
        font-size: 12px;
        color: #9ea0ad;
        .icon-xiangyou1 {
          font-size: 10px;
        }
      }
      .i {
        position: absolute;
        left: 0;
        top: 5px;
        width: 4px;
        height: 18px;
        background-color: #f1325e;
      }
    }
    .play_img {
      width: 100%;
      margin-top: 18px;
      border-radius: 6px;
      overflow: hidden;
    }
    .free_box {
      display: flex;
      flex-wrap: wrap;
      .free_item {
        margin-top: 19px;
        width: 100px;
        // margin-right: 17px;
        margin-right: calc((100% - 300px) / 2);
        &:nth-child(3n) {
          margin-right: 0;
        }
        .img {
          width: 100%;
          position: relative;
          height: 130px;
          .over {
            position: absolute;
            bottom: 8px;
            right: 7px;
            color: #fff;
            opacity: 0.7;
            background-color: rgba(0, 0, 0, 0.5);
            font-size: 11px;
            height: 20px;
            line-height: 20px;
            padding: 0 5px;
            border-radius: 2px;
          }
        }
        .p {
          display: flex;
          flex-direction: column;
          margin-top: 6px;
          .name {
            font-size: 12px;
            color: #686878;
          }
          .names {
            transform: scale(0.6);
            color: #aaaaad;
            margin-left: -24px;
          }
        }
      }
    }
  }
  .serials {
    margin: 0 20px;
    margin-top: 16px;
    border-top: 1px dashed #e9e9e9;
    padding-top: 17px;
    .serials_item {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      margin-bottom: 15px;
      .wem {
        display: flex;
        .left {
          color: #ceb189;
          margin-right: 8px;
        }
        .left_s {
          color: #7b797a;
        }
      }
      .over {
        display: flex;
        .right_s {
          color: #ceb189;
          margin-left: 8px;
        }
        .right {
          color: #bebac3;
        }
        .over_right {
          color: #7b797a;
          margin-left: 8px;
        }
      }
    }
  }
}
::v-deep .van-swipe__indicator.van-swipe__indicator--active {
  width: 12px;
  border-radius: 4px;
  height: 3px;
}
::v-deep .van-swipe__indicator {
  width: 4px;
  height: 4px;
}

.my-swipe-bg {
  position: absolute;
  top: 0;
  bottom: 0;
  height: max-content;
  margin: auto;
  left: 0;
  right: 0;
  z-index: -1;
  transform: rotateY(0deg) scale(1.3);
  filter: blur(8px);
}
</style>